<!--
  -    Copyright (c) 2018-2025, infomng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: infomng
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        :before-open="beforeOpen"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="handleDel">
        <template slot="menuLeft">
         <!-- <el-button
            v-if="permissions.workorder_personmanage_export"
            class="filter-item"
            plain
            type="primary"
            size="small"
            icon="el-icon-download"
            @click="exportExcel">导出
          </el-button>-->
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text"  icon="el-icon-edit"  size="small" plain  @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text"  icon="el-icon-view"  size="small" plain  @click="trainInfoOpen(scope.row)">培训信息</el-button>
          <el-button type="text"  icon="el-icon-delete"  size="small" plain  @click="handleDel(scope.row,scope.index)">删除</el-button>
        </template>
        <template slot-scope="scope" slot="personNameForm">
          <div class="block">
            <el-select  v-model="personName" filterable clearable placeholder="请选择人员"  @clear="clearPerson"style="width: 100%">
              <el-option
                @click.native="choosePerson(item)"
                v-for="item in personCredentialList"
                :label="item.personName"
                :value="item.personName"
                :key="item.personName">
              </el-option>
            </el-select>
          </div>
        </template>
        <template slot-scope="scope" slot="shiftIdForm">
          <div class="block">
            <el-select  v-model="shiftId" filterable clearable placeholder="请选择班次"  @clear="clearShift" style="width: 100%">
              <el-option
                @click.native="chooseShift(item)"
                v-for="item in shiftManageList"
                :label="item.shiftName"
                :value="item.id"
                :key="item.id">
              </el-option>
            </el-select>
          </div>
        </template>
        <template slot-scope="scope" slot="productLineNameForm">
          <div class="block">
          <avue-input v-model="productLineName" disabled/>
          </div>
        </template>
        <template slot-scope="scope" slot="workshopSectionForm">
          <div class="block">
            <el-input  v-model="workshopSection" filterable clearable disabled placeholder="请选择工段"   style="width: 100%"></el-input>
          </div>
        </template>

        <template slot-scope="scope" slot="processIdForm">
          <div class="block">
            <el-select  v-model="processId" filterable clearable placeholder="请选择工序"   @clear="clearProcess"  style="width: 100%">
              <el-option
                @click.native="chooseProcess(item)"
                v-for="item in processList"
                :label="item.processName"
                :value="item.processId"
                :key="item.processId">
              </el-option>
            </el-select>
          </div>
        </template>

      </avue-crud>


      <!--培训记录弹窗页面-->
      <el-dialog title=""
                 :visible.sync="trainDialogVisible"
                 width="50%"
                 lock-scroll
                 v-if="trainDialogVisible"
                 :fullscreen="trainDialogFull">
        <el-backtop target=".el-card__body .el-dialog__wrapper .el-dialog" :right="15" :bottom="20" :visibility-height="1" />
        <el-backtop :bottom="60"></el-backtop>
        <template slot="title">
          <div class="avue-crud__dialog__header">
            <span class="el-dialog__title">
                <span style="display:inline-block;background-color: #3478f5;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px"></span>
               培训人员：{{personName}}<i class="el-breadcrumb__separator el-icon-arrow-right"></i>培训记录
            </span>
          </div>
        </template>
        <!--人员信息页面-->
        <trainInfo ref="trainRef"  :personIdStr="personId"  />
      </el-dialog>


    </basic-container>
  </div>
</template>

<script>
import { fetchList, addObj, putObj, delObj ,getWorkshopSectionByLine} from '@/api/workorder/personmanage'
import { tableOption } from '@/const/crud/workorder/personmanage'
import {getShiftManageList} from '@/api/workorder/shiftmanage'
import {getRealityProductLineDetailsList} from '@/api/pfoc/realityproductlinemain' //获取产线工序
import { getPersonCredentialList } from '@/api/workorder/personcredentialmanage' //获取所有人员数据
/**引入培训信息页面*/
import trainInfo from '@/views/workorder/personmanage/trainrecord'

import { mapGetters } from 'vuex'

export default {
  name: 'personmanage',
    components: {
        //培训息页面
        trainInfo,
    },
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
        shiftManageList:[],//班次集合
        shiftId:null,//班次ID
        shiftName:null,//班次名称
        productLineId:null,//产线ID
        productLineName:null,//产线名称
        workshopSection:null,//工段
        workshopSectionList:[],//工段集合
        processId:null,//工序ID
        processName:null,//工序名称
        processList:[],//工序集合
        personCredentialList:[],//人员资质数据集
        personName:null,//人员名称
        personId:null,//人员资质ID，
        trainDialogVisible:false,//人员信息页面开关
        trainDialogFull:true,//人员信息页面弹窗缩放开关（默认放大）


    }
  },

  created() {
      //获取班次集合
      this.getShiftManageListData();
      //获取所有人员数据
      this.getPersonCredentialListData();

  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.workorder_personmanage_add, false),
        delBtn: this.vaildData(this.permissions.workorder_personmanage_del, false),
        editBtn: this.vaildData(this.permissions.workorder_personmanage_edit, false)
      }
    }
  },
  methods: {
    //获取班次
    getShiftManageListData(){
        getShiftManageList().then(response => {
            this.shiftManageList = response.data.data;
        })
    },
    //获取所有人员数据
    getPersonCredentialListData(){
        getPersonCredentialList().then(response => {
            this.personCredentialList = response.data.data;
        })

    },
    //人员选择
    choosePerson(row){
        //员工号
        var jobNo = this.findObject(this.tableOption.column, 'jobNo');
        this.$set(this.$refs['crud'].tableForm, 'jobNo', row.jobNo);
        //资质等级
        var credentialGrade = this.findObject(this.tableOption.column, 'credentialGrade');
        this.$set(this.$refs['crud'].tableForm, 'credentialGrade', row.credentialGrade);
        //厂内技能
        var skill = this.findObject(this.tableOption.column, 'skill');
        this.$set(this.$refs['crud'].tableForm, 'skill', row.skill);
        //人员名称
        var personName = this.findObject(this.tableOption.column, 'personName');
        personName.value =  row.personName;//必须有（相当于开启表单当前属性）
        this.$set(this.$refs['crud'].tableForm, 'personName', row.personName);

        this.personId = row.id;

    },
    //人员清空
    clearPerson(row){
        //员工号
        var jobNo = this.findObject(this.tableOption.column, 'jobNo');
        this.$set(this.$refs['crud'].tableForm, 'jobNo', null);
        //资质等级
        var credentialGrade = this.findObject(this.tableOption.column, 'credentialGrade');
        this.$set(this.$refs['crud'].tableForm, 'credentialGrade', null);
        //厂内技能
        var skill = this.findObject(this.tableOption.column, 'skill');
        this.$set(this.$refs['crud'].tableForm, 'skill', null);
        //人员名称
        var personName = this.findObject(this.tableOption.column, 'personName');
        this.$set(this.$refs['crud'].tableForm, 'personName', null);
        this.personId = null;
        this.personName = null;

    },
    //班次选择
    chooseShift(row){
        if(row.shiftName != this.shiftName){
            this.clearProcess();
        }
        this.shiftName = row.shiftName;//班次名称
        this.productLineId = row.productLineId;//产线ID
        this.productLineName = row.productLineName;//产线名称
        //获取工段集合
        getWorkshopSectionByLine(row.productLineId).then(response => {
          this.workshopSectionList = response.data.data;
      })
        //获取产线工序集合
        getRealityProductLineDetailsList( row.productLineId).then(response => {
            this.processList =response.data.data.realityProductLineDetailsList;//获取产线工序
        })

    },
    //班次清空
    clearShift(){
        this.shiftName =null;//清空班次
        this.shiftId =null;//清空班次
        this.productLineId = null;//产线ID
        this.productLineName =null;//产线名称
        this.workshopSection = null;//清空工段
        this.processId=null;//工序ID
        this.processName=null;//工序名称
    },
    //工序选择
    chooseProcess(row){
       this.processId=row.processId;//工序ID
       this.processName=row.processName;//工序名称
       this.workshopSection = row.workshopSection;//工段
    },
    //工序清空
    clearProcess(){
        this.processId=null;//工序ID
        this.processName=null;//工序名称
        this.workshopSection = null;//工段
    },
    //表单打开之前清空重置
    beforeOpen(done,type){
        if (["add"].includes(type)) {
            this.$nextTick(() => {
                var form =  this.$refs['crud'].tableForm;
                Object.keys(form).forEach((key) =>{
                    form[key] = null;

                })
            })
            this.clearShift();
            this.clearPerson();
        }
        done();
    },
    //默认编辑方法
    handleEdit:function(row,index){
        this.personId = row.personId;//人员ID
        this.personName = row.personName;//人员
        this.shiftName =row.shiftName;//班次
        this.shiftId =row.shiftId;//班次Id
        this.productLineId = row.productLineId;//产线ID
        this.productLineName =row.productLineName;//产线名称
        this.workshopSection = row.workshopSection;//工段
        this.processId=row.processId;//工序ID
        this.processName=row.processName;//工序名称
        if(!this.validatenull(this.productLineId)) {
            //获取工段集合
            getWorkshopSectionByLine(row.productLineId).then(response => {
                this.workshopSectionList = response.data.data;
            })
            //获取产线工序集合
            getRealityProductLineDetailsList(row.productLineId).then(response => {
                this.processList = response.data.data.realityProductLineDetailsList;//获取产线工序
            })
        }
        this.$refs.crud.rowEdit(row, index);

    },
    // 列表查询
    getList(page, params) {
      this.tableLoading = true
      fetchList(Object.assign({
        descs:"sort_order",
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 删除
    handleDel: function(row, index) {
      this.$confirm('是否确认删除人员【' + row.personName+'】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },

    // 更新
    handleUpdate: function(row, index, done, loading) {
        row.shiftName =this.shiftName;//班次
        row.shiftId =this.shiftId;//班次ID
        row.productLineId =  this.productLineId;//产线ID
        row.productLineName =this.productLineName;//产线名称
        row.workshopSection =  this.workshopSection ;//工段
        row.processId=this.processId;//工序ID
        row.processName=this.processName;//工序名称
        row.personId = this.personId;//人员ID
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 保存
    handleSave: function(row, done, loading) {
        row.personId = this.personId;//人员ID
        row.shiftName =this.shiftName;//班次
        row.shiftId =this.shiftId;//班次
        row.productLineId =  this.productLineId;//产线ID
        row.productLineName =this.productLineName;//产线名称
        row.workshopSection =  this.workshopSection ;//工段
        row.processId=this.processId;//工序ID
        row.processName=this.processName;//工序名称
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    // 当前页发生改变事件
    currentChange(current) {
      this.page.currentPage = current
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page)
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile('/workorder/personmanage/export', this.searchForm, 'personmanage.xlsx')
    },
    //打开培训信息窗口
    trainInfoOpen(row){
        this.trainDialogVisible=true;//培训信息页面开关
        this.personId = row.personId;//人员ID
        this.personName = row.personName;//人员
        this.$nextTick(() => {
            //调用培训信息查询方法
            this.$refs.trainRef.refreshChange();
        })
    }
  }
}
</script>
